package com.cherrish.demo;

import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;

/**
 * @author cherrish
 * @time 2019-04-04 18:06
 * @name MapperedByteBufferTest
 * @desc:
 */
public class MapperedByteBufferTest {
    public static void main(String[] args) {
        File file = new File("D:\\workspace\\test\\001\\do-demo\\demo-test\\demo1.txt");
        long len = file.length();
        byte[] bs = new byte[(int)len];
        try {
            MappedByteBuffer mappedByteBuffer = new RandomAccessFile(file, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, 0, len);
            for(int offset = 0; offset < len; offset++){
                byte b = mappedByteBuffer.get();
                bs[offset] = b;
            }
            Scanner scanner = new Scanner(new ByteArrayInputStream(bs)).useDelimiter(" ");
            while (scanner.hasNext()){
                System.out.println(scanner.next() + " ");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
